import base64

def handle_time_remind(param):

	clock1=param['clock1']
	clock2=param['clock2']
	clock3=param['clock3']
	clock1_sw=param['clock1_sw']
	clock2_sw=param['clock2_sw']
	clock3_sw=param['clock3_sw']
	clock_type={'alarm_one':'1','customize':'3','alarm_everyday':'2'}

	def get_customize(rate,customize):
		res=list('0000000')
		if rate=='3':
			for x in customize.split(','):
				res[int(x)-1]='1'
			res=''.join(res)
			return res
		else:
			return None

	clock1_rate=clock_type[param['clock1_rate']]
	clock2_rate=clock_type[param['clock2_rate']]
	clock3_rate=clock_type[param['clock3_rate']]
	colck1_param=clock1+'-'+clock1_sw+'-'+clock1_rate
	if get_customize(clock1_rate,param['customize1'])!=None:
		colck1_param=clock1+'-'+clock1_sw+'-'+clock1_rate+'-'+get_customize(clock1_rate,param['customize1'])
	colck2_param=clock2+'-'+clock2_sw+'-'+clock2_rate
	if get_customize(clock2_rate,param['customize2'])!=None:
		colck2_param=clock2+'-'+clock2_sw+'-'+clock2_rate+'-'+get_customize(clock2_rate,param['customize2'])
	colck3_param=clock3+'-'+clock3_sw+'-'+clock3_rate
	if get_customize(clock3_rate,param['customize3'])!=None:
		colck3_param=clock3+'-'+clock3_sw+'-'+clock3_rate+'-'+get_customize(clock3_rate,param['customize3'])
	res_param=colck1_param+','+colck2_param+','+colck3_param
	return res_param


def three_command(device_id,kind,param,model,sender='admin'):
	command=''
	title_dict={'send_rate':'UPLOAD','health_rate':'hrtstart','center_num_set':'CENTER','phone_call':'CALL',
				'sos1':'SOS1','sos2':'SOS2','sos3':'SOS3','sos':'SOS','ip_port':'IP','time_language':'LZ','sosmsg_to_phone':'SOSSMS',
				'low_power_msg':'LOWBAT','dev_remove_alarm':'REMOVE','dev_remove_msg':'REMOVESMS','step_cunt_btn':'PEDO','walk_time_set':'WALKTIME',
				'silence_time_set':'SILENCETIME','time_remind':'REMIND','sleep_time':'SLEEPTIME','work_model':'profile'}
	title_list={'call_now':'CR','factory_set':'FACTORY','verson_detail':'VERNO','dev_reset':'RESET','turn_off_dev':'POWEROFF','find_dev':'FIND',
				'photograph':'rcapture','monitor':'MONITOR','health_detect':'hrtstart'}
	dialog_talk={'voice':'TK','text':'MESSAGE','picture':'wdimg'}
	data_len=''
	if kind in title_dict:
		if kind=='sos':
			sos1=param['sos1']
			sos2=param['sos2']
			sos3=param['sos3']
			if param['sos1_sw']!='on':
				sos1='0'
			if param['sos2_sw']!='on':
				sos2='0'
			if param['sos3_sw']!='on':
				sos3='0'
			param=sos1+','+sos2+','+sos3
		elif kind=='time_remind':
			param=handle_time_remind(param)
			
		kind=title_dict[kind]
		data_len=len(kind)+1+len(param)
		data_len= '%04x' % data_len
		command=b'[3G*'+device_id.encode()+b'*'+data_len.encode()+b'*'+kind.encode()+b','+param.encode()+b']'

	elif kind in title_list:
		kind=title_list[kind]
		if kind=='hrtstart':
			kind='hrtstart,1'
		data_len=len(kind)
		data_len= '%04x' % data_len
		command=b'[3G*'+device_id.encode()+b'*'+data_len.encode()+b'*'+kind.encode()+b']'

	elif kind in dialog_talk:
		if kind=='text':
			handle_param=''
			for x in range(len(param)):
				first=hex(ord(param[x]))[2:]
				for y in range(4-len(first)):
					first='0'+first
				handle_param+=first
			param=handle_param.encode('gbk')
		else:
			param=param.split(',')[1]
			replace_dict1={b'}':b'}\x01',b'[':b'}\x02',b']':b'}\x03',b',':b'}\x04',b'*':b'}\x05'}
			param=base64.b64decode(param)
			for repl_tag in replace_dict1:
				param=param.replace(repl_tag,replace_dict1[repl_tag])
			
		kind=dialog_talk[kind]
		data_len=len(kind)+1+len(param)
		data_len= '%04x' % data_len
		command=b'[3G*'+device_id.encode()+b'*'+data_len.encode()+b'*'+kind.encode()+b','+param+b']'

	elif kind in ['phonebook','del_phonebook']:
		comm_dict={'del_phonebook':'DPHBX','phonebook':'PHBX'}
		param=param.split(',')
		send_info=param
		param=param[1]
		handle_param=''
		for x in range(len(param)):
			first=hex(ord(param[x]))[2:]
			for y in range(4-len(first)):
				first='0'+first
			handle_param+=first
		p_name=handle_param.encode('gbk')

		send_info=send_info[0].encode()+b','+p_name+b','+send_info[2].encode()
		data_len=len(comm_dict[kind])+1+len(send_info)
		print(len(send_info))
		data_len= '%04x' % data_len

		command=b'[3G*'+device_id.encode()+b'*'+data_len.encode()+b'*'+comm_dict[kind].encode()+b','+send_info+b']'


	return command


# data=dict(clock1= "10:07",
# clock1_sw= "0",
# clock1_rate= "alarm_one",
# customize1= "",
# clock1_type= "clock",
# clock2= "10:15",
# clock2_sw= "1",
# clock2_rate= "alarm_everyday",
# customize2= "",
# clock2_type= "clock",
# clock3= "10:16",
# clock3_sw= "1",
# clock3_rate= "customize",
# customize3= "1,2",
# clock3_type= "clock")


# ress=three_command('device_id','time_remind',data,'three',sender='admin')
# print(ress)